package com.gmu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gmu.domain.ResResult;
import com.gmu.listener.UnitsListener;
import com.gmu.pojo.TblRole;
import com.gmu.pojo.TblUnits;
import com.gmu.mapper.TblUnitsMapper;
import com.gmu.pojo.excel.UnitsData;
import com.gmu.pojo.excel.UserData;
import com.gmu.pojo.vo.units.AcademyVo;
import com.gmu.pojo.vo.units.ClassVo;
import com.gmu.pojo.vo.units.MajorVo;
import com.gmu.pojo.vo.units.SchoolVo;
import com.gmu.service.TblRoleService;
import com.gmu.service.TblUnitsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 单位表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Service
public class TblUnitsServiceImpl extends ServiceImpl<TblUnitsMapper, TblUnits> implements TblUnitsService {


    @Autowired
    private TblRoleService tblRoleService;

    @Autowired
    private TblUnitsMapper tblUnitsMapper;

    //获取所有学校分类
    @Override
    public List<SchoolVo> getAllSchool(Integer roleId) {
        QueryWrapper<TblUnits> wrapper = new QueryWrapper<>();
        wrapper.lt("parent_id",0);
        List<TblUnits> allSchool = baseMapper.selectList(wrapper);
        List<SchoolVo> schoolList = new ArrayList<>();
        for (int i = 0; i < allSchool.size(); i++) {
            //获取units_code
           String[] unitsCode = allSchool.get(i).getUnitsCode().split("\\.");
           //获取学校的parentId
           // System.out.println(unitsCode.length);
           int parentId = Integer.parseInt(unitsCode[unitsCode.length-1]);
            QueryWrapper<TblUnits> schoolWrapper = new QueryWrapper<>();
            schoolWrapper.eq("parent_id",parentId);
            List<TblUnits> school = baseMapper.selectList(schoolWrapper);
            for (int j = 0; j < school.size(); j++) {
                SchoolVo schoolVo = new SchoolVo();
                schoolVo.setName(school.get(j).getName());
                String[] academyCode = school.get(j).getUnitsCode().split("\\.");
                int codeId = Integer.parseInt(academyCode[academyCode.length-1]);
                schoolVo.setUnitId(codeId);
                schoolList.add(schoolVo);
                QueryWrapper<TblUnits> academyWrapper = new QueryWrapper<>();
                academyWrapper.eq("parent_id",codeId);
                List<TblUnits> academy = baseMapper.selectList(academyWrapper);
                List<AcademyVo> academyList = new ArrayList<>();
                for (int k = 0; k < academy.size(); k++) {
                    AcademyVo academyVo = new AcademyVo();
                    academyVo.setName(academy.get(k).getName());
                    String[] majorCode = academy.get(k).getUnitsCode().split("\\.");
                    int codeMajorId = Integer.parseInt(majorCode[majorCode.length-1]);
                    academyVo.setUnitId(codeMajorId);
                    academyList.add(academyVo);
                    QueryWrapper<TblUnits> majorWrapper = new QueryWrapper<>();
                    majorWrapper.eq("parent_id",codeMajorId);
                    List<TblUnits> majorData = baseMapper.selectList(majorWrapper);
                    List<MajorVo> majorList = new ArrayList<>();
                    for (int m = 0; m < majorData.size(); m++) {
                        MajorVo majorVo = new MajorVo();
                        majorVo.setName(majorData.get(m).getName());
                        String[] classCode = majorData.get(m).getUnitsCode().split("\\.");
                        int codeClassId = Integer.parseInt(classCode[classCode.length-1]);
                        majorVo.setUnitId(codeClassId);
                        majorList.add(majorVo);
                        QueryWrapper<TblUnits> classWrapper = new QueryWrapper<>();
                        classWrapper.eq("parent_id",codeClassId).eq("role_id",roleId);
                        List<TblUnits> classData = baseMapper.selectList(classWrapper);
                        List<ClassVo> classList = new ArrayList<>();
                        for (TblUnits classDatum : classData) {
                            ClassVo classVo = new ClassVo();
                            classVo.setName(classDatum.getName());
                            classList.add(classVo);
                        }
                        majorVo.setClassList(classList);
                    }
                    academyVo.setMajorList(majorList);
                }
                schoolVo.setAcademyList(academyList);
            }
        }
        return schoolList;
    }

    @Override
    public ResResult<?> importUnits(MultipartFile file) {
        List<TblRole> roleList = tblRoleService.list();
        //excel将数据导入数据库
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, UnitsData.class,new UnitsListener(tblUnitsMapper,roleList)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ResResult<>(200,"导入成功");

    }
}
